#ifndef __THERMOMETER_H__
#define __THERMOMETER_H__

#include <Time.h>
#include <Arduino.h>
#include <DallasTemperature.h>

#include "Errors.h"
#include "Actuator.h"
#include "Storage.h"

#define THERMOMETER_RESOLUTION 10

typedef float temperature_t;

static DeviceAddress WATER_TEMPERATURE_ADDRESS = {0x28, 0xFF, 0x42, 0xE8, 0x4E, 0x04, 0x00, 0x89};

class Thermometer : public Actuator, public CommandProcessor {
protected:
    String name;

    time_t lastUpdateTime;
    temperature_t lastTemperature;

public:
    Thermometer(const char *name);

    virtual ~Thermometer();


    const time_t getLastUpdate() const;

    const temperature_t getValue() const;

public:
    virtual const bool isSupported(String const &request);

    virtual const error_t processCommand(String const &request, String &response);
};


// IMPL: http://forum.arduino.cc/index.php?topic=157137.0
class DallasThermometer : public Thermometer {
private:
    int responseTimeout; // calculated value based on resolution
    unsigned long requestTime;

private:
    OneWire *wire;
    DeviceAddress address;
    DallasTemperature *sensor;

private:
    void sendRequest();

public:
    DallasThermometer(const char *name, uint8_t pin, DeviceAddress address);

    ~DallasThermometer();

    const uint8_t getResolution();

    void setResolution(const uint8_t resolution);

public:
    virtual const error_t begin();

    virtual const error_t tick(const tick_t ticks, const time_t time);

public:
    virtual const error_t processCommand(String const &request, String &response);
};

#endif